/**
 * @overview 创建线
 * @author dengqiang
 * @description 鲸云CgsLine
 * @update  2022.04.14
 */
class GPolyline {
  constructor() {
    window.GPolylineArray = []
  }

  //新增单条线
  add(paramObj) {
    const { name, parentName, option } = paramObj
    const optionModified = {
      name: option.name,
      positions: option.posArray,
      followSurface: false,
      width: option.width || 8,
      fillColor: option.fillColor || 'rgba(77, 212, 100, 1.0)',
      outlineColor: option.outlineColor || 'rgba(0, 255, 0, 1.0)',
      lineType: option.lineType || 2,
      outlineWidth: option.outlineWidth || 5,
    }
    const polyline = new CgsLine(optionModified)

    const polylineObject = {
      name: name,
      parentName: parentName,
      polyline: polyline,
    }
    window.GPolylineArray.push(polylineObject)
    window.map.addFeature(polyline)
  }

  //根据name移除单条线
  removeByName(name) {
    window.GPolylineArray.forEach((item, index) => {
      if (item.name == name) {
        window.map.removeFeature(item.polyline)
        window.GPolylineArray.splice(index, 1)
      }
    })
  }

  //根据parentName移除一批线
  removeByPatName(parentName) {
    const length = window.GPolylineArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GPolylineArray[i].parentName == parentName) {
        window.map.removeFeature(window.GPolylineArray[i].polyline)
        window.GPolylineArray.splice(i, 1)
      }
    }
  }

  //移除所有线
  removeAll() {
    window.GPolylineArray.forEach((item) => {
      window.map.removeFeature(item.polyline)
    })
    window.GPolylineArray = []
  }

  //根据name控制单条线的显隐
  setVisibleByName(name, boolean) {
    window.GPolylineArray.forEach((item) => {
      if (item.name == name) {
        item.polyline.setVisible(boolean)
      }
    })
  }

  //根据parentName控制一批线的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GPolylineArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.polyline.setVisible(boolean)
      }
    })
  }

  //根据name设置单条线的位置
  setPosition(name, newPosition) {
    window.GPolylineArray.forEach((item, index) => {
      if (item.name == name) {
        item.polyline.setPosition(newPosition)
      }
    })
  }
}

export default GPolyline
